package com.zlzlib.libnetty.tcp.handler;


import com.zlzlib.libnetty.base.BaseClient;
import com.zlzlib.libnetty.util.Util;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by zlz on 2019/11/19.
 * 心跳消息响应处理handler
 */
public class HeartbeatRespHandler<T> extends ChannelInboundHandlerAdapter {

    private final BaseClient<T> imsClient;

    public HeartbeatRespHandler(BaseClient<T> imsClient) {
        this.imsClient = imsClient;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        T msg = imsClient.getDataListener().changeMsgData(obj);
        if (imsClient.getDataListener().isDataEmpty(msg)) {
            return;
        }
        if (imsClient.getDataListener().isHeartbeatMsg(msg)) {
            Util.logInfo("收到服务端心跳响应消息，message=" + msg);
        } else {
            // 消息透传
            ctx.fireChannelRead(msg);
        }
    }

}
